﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AllReturnToFactoryDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));

            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);

            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                connectPersonLabel.Text = provider.Contact1Name;
                if (!string.IsNullOrEmpty(provider.Contact2Name))
                {
                    connectPersonLabel.Text += "; " + provider.Contact2Name;
                }
                providerCodeLabel.Text = provider.ProviderCode;
                addressLabel.Text = provider.Address;
                phoneLabel.Text = provider.Contact1Phone;
                if (!string.IsNullOrEmpty(provider.Contact2Phone))
                {
                    phoneLabel.Text += "; " + provider.Contact2Phone;
                }

                phoneLabel.Text += "; " + provider.Contact1MobilePhone;
                if (!string.IsNullOrEmpty(provider.Contact2MobilePhone))
                {
                    phoneLabel.Text += "; " + provider.Contact2MobilePhone;
                }
                emailLabel.Text = provider.Contact1Email;
                if (!string.IsNullOrEmpty(provider.Contact2Email))
                {
                    emailLabel.Text += "; " + provider.Contact2Email;
                }
            }

            runningNumberLabel.Text = form.RunningNumber;
            facotryRMALabel.Text = form.Code;

            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            applicantEmailLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserEmail;
            applicantLabel.Text = form.ApplicationProduct.ApplicationForm.User.ContactPerson;
            customerNameLbl.Text = form.ApplicationProduct.ApplicationForm.User.FullName;
            applicantPhoneLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserPhone;

            rtfDateTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            //receiveTimeLabel.Text = form.ApplicationProduct.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd");
            if (!form.ReturnToFactoryQuatation.IsLoaded)
            {
                form.ReturnToFactoryQuatation.Load();
            }
            if (form.ReturnToFactoryQuatation.Count > 0)
            {
                reasonLabel.Text = form.QuatationReason;
            }

            quotationDateLabel.Text = form.ProviderQuatationDate.HasValue ? form.ProviderQuatationDate.Value.ToString("yyyy-MM-dd") : "";
            customerQuatationDateLbl.Text = form.ApplicationProduct.QuotationDateTime.HasValue ? form.ApplicationProduct.QuotationDateTime.Value.ToString("yyyy-MM-dd") : "";
            if (!form.ApplicationProduct.QuatationForm.IsLoaded)
            {
                form.ApplicationProduct.QuatationForm.Load();
            }
            if (form.ApplicationProduct.QuatationForm.Count > 0)
            {
                customerQuatationReasonLbl.Text = form.ApplicationProduct.QuatationForm.First().Reason;
            }
            //if (form.ApplicationProduct.Cost.HasValue)
            //{
            //    costLabel.Text = form.ApplicationProduct.Cost.ToString();
            //}
            if (form.ApplicationProduct.IsDecideToRepair.HasValue && !form.ApplicationProduct.IsDecideToRepair.Value)
            {
                customerDecisionLbl.Text = "不维修";
            }
            else
            {
                customerDecisionLbl.Text = "确认维修";
            }
            if (form.DoesRepair.HasValue && !form.DoesRepair.Value)
            {
                factoryRepairLbl.Text = "不维修";
            }
            else
            {
                factoryRepairLbl.Text = "确认维修";
            }
            if (form.IsSecondTimeRepair.HasValue && form.IsSecondTimeRepair.Value)
            {
                isSecondTimeFC.Text = "是";
            }
            else
            {
                isSecondTimeFC.Text = "否";
            }
            confirmProviderDateLbl.Text = form.ConfirmProviderDate.HasValue ? form.ConfirmProviderDate.Value.ToString("yyyy-MM-dd") : "";
            confirmRMADateLbl.Text = form.ConfirmCodeDate.HasValue ? form.ConfirmCodeDate.Value.ToString("yyyy-MM-dd") : "";
            sendDateLbl.Text = form.SendDate.HasValue ? form.SendDate.Value.ToString("yyyy-MM-dd") : "";
            factoryReceiveDateLbl.Text = form.FactoryReceiveDate.HasValue ? form.FactoryReceiveDate.Value.ToString("yyyy-MM-dd") : "";
            receiveDateLbl.Text = form.ReceiveDate.HasValue ? form.ReceiveDate.Value.ToString("yyyy-MM-dd") : "";
            checkDateLb.Text = form.CheckDate.HasValue ? form.CheckDate.Value.ToString("yyyy-MM-dd") : "";
            checkremarklb.Text = form.CheckRemark;
            receiveRemarklb.Text = form.ReceiveRemark;
            statusLbl.Text = form.Status;
            BindCustomerQuatationList();
            BindProviderQuatationList();
        }

        private void BindCustomerQuatationList()
        {
            ApplicationProductService productService = new ApplicationProductService();
            ReturnToFactoryService returnFactoryService = new ReturnToFactoryService();
            ReturnToFactoryForm returnFactoryForm = returnFactoryService.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            if (!returnFactoryForm.ApplicationProductReference.IsLoaded)
            {
                returnFactoryForm.ApplicationProductReference.Load();
            }
            CustomerQuatationList.DataSource = productService.GetQuatationFormByProductID(returnFactoryForm.ApplicationProduct.ApplicationProductID);
            CustomerQuatationList.DataBind();
        }

        private void BindProviderQuatationList()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ProviderQuatationGridView.DataSource = service.GetQuatationList(Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            ProviderQuatationGridView.DataBind();
        }
    }
}
